package com.winit.openapi.lma.controller.validator;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.ValidateUtil;

public class LMAShipmentOrderValidator {

    /**
     * 校验发货订单信息
     * 
     * @param json
     */
    public static void validateCreateShipmentOrder(JSONObject json) {

        ValidateUtil.validImportNotNull(json, "sellerOrderNo", "sellerOrderNo");
        ValidateUtil.validImportNotNull(json, "productInfo", "productInfo");
        ValidateUtil.validImportNotNull(json.getJSONObject("productInfo"), "winitProductCode", "winitProductCode产品编码");
        
        ValidateUtil.validImportNotNull(json, "shipper", "shipper");
        ValidateUtil.validImportNotNull(json.getJSONObject("shipper"), "pickupAddressCode", "pickupAddressCode");

//        ValidateUtil.validImportNotNull(json, "totalParcelsWeight", "totalParcelsWeight");

        ValidateUtil.validImportNotNull(json, "receiver", "receiver");
        JSONObject receiver = json.getJSONObject("receiver");
        ValidateUtil.validImportNotNull(receiver, "receiverName", "receiverName");
        ValidateUtil.validImportNotNull(receiver, "phoneNo", "phoneNo");
        ValidateUtil.validImportNotNull(receiver, "postCode", "postCode");
        ValidateUtil.validImportNotNull(receiver, "countryCode", "countryCode");
        ValidateUtil.validImportNotNull(receiver, "city", "city");
        ValidateUtil.validImportNotNull(receiver, "addressLine1", "addressLine1");
        ValidateUtil.validEmailOtherReminder(receiver, "email", "email");
        ValidateUtil.validImportNotNull(json, "totalParcelsNumber", "totalParcelsNumber");
        ValidateUtil.validInteger(json, "totalParcelsNumber", "totalParcelsNumber");

        ValidateUtil.validImportMaxLength(json, "shiperMemo", "shiperMemo", 100);
        ValidateUtil.validImportMaxLength(json, "phoneNo", "phoneNo", 20);

        ValidateUtil.validImportNotNull(json, "parcelList", "包裹");
        ValidateUtil.validListOtherReminder(json, "parcelList", "包裹");
        JSONArray parcelList = json.getJSONArray("parcelList");

        if (parcelList == null || parcelList.size() == 0) {
            throw new ApiException(ErrorCode.PACKAGE_CAN_NOT_NULL);
        }
        if (parcelList.size() > 1) {
            throw new ApiException(ErrorCode.PACKAGE_ONLY_ONE);
        }
        for (Object obj : parcelList) {
            JSONObject pack = (JSONObject) obj;
            ValidateUtil.validImportNotNull(pack, "weight", "重量");
            ValidateUtil.validImportNotNull(pack, "length", "长");
            ValidateUtil.validImportNotNull(pack, "width", "宽");
            ValidateUtil.validImportNotNull(pack, "height", "高");
//            ValidateUtil.validImportNotNull(pack, "parcelNo", "parcelNo");

            ValidateUtil.validImportMaxLength(pack, "parcelDesc", "包裹描述", 60);
            ValidateUtil.validImportMaxLength(pack, "parcelNo", "包裹号", 20);

            JSONArray merchandiseList = pack.getJSONArray("merchandiseList");
            for (Object o : merchandiseList) {
                JSONObject merchandise = (JSONObject) o;
                ValidateUtil.validImportNotNull(merchandise, "declaredNameEn", "申报品名(英文)");
                ValidateUtil.validImportNotNull(merchandise, "declaredCurrency", "申报币种");
                ValidateUtil.validImportNotNull(merchandise, "declaredValue", "申报价值");
                ValidateUtil.validImportNotNull(merchandise, "quantity", "数量");
                
                //需求9254 优化LMA创建货单接口（lma.shipment.create）入参商品编码“merchandiseCode”必填验证 2017-11-16
                ValidateUtil.validImportNotNull(merchandise, "merchandiseCode", "商品编码");
                ValidateUtil.validImportMaxLength(merchandise, "merchandiseCode", "商品编码", 64);

                ValidateUtil.validImportMaxLength(merchandise, "transactionID", "交易ID", 30);
                ValidateUtil.validImportMaxLength(merchandise, "itemID", "条目ID", 30);
                ValidateUtil.validImportMaxLength(merchandise, "merchandiseName", "商品名称", 255);
                ValidateUtil.validImportMaxLength(merchandise, "merchandiseCode", "商品编码", 255);
                ValidateUtil.validImportMaxLength(merchandise, "isHasBattery", "是否含有电池", 10);
                ValidateUtil.validImportMaxLength(merchandise, "quantity", "数量", 8);
                ValidateUtil.validImportMaxLength(merchandise, "specifications", "商品规格", 255);
            }
        }
    }
}
